﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace TerryDong.Core
{
    public class XmlConfigHelper
    {
        private string fileName = string.Empty;
        private Dictionary<string, XmlConfigModel> dict = null;
        private XmlDocument doc = null;
        private XmlNode node = null;
        public XmlConfigHelper(string filePath)
        {
            fileName = filePath;
            dict = new Dictionary<string, XmlConfigModel>();

            doc = new XmlDocument();
            doc.Load(filePath);

            ConfigXml = doc.InnerXml;

            node = doc.SelectSingleNode("/configuration/appSettings");

            if (node==null||node.ChildNodes.Count<1)
            {
                throw new Exception("appSettings has no childs elements");
            }
            string key = string.Empty;
            XmlConfigModel xmlConfigModel = null;
            string value = string.Empty;
            foreach (XmlNode childNode in node.ChildNodes)
            {
                if (dict!=null && !dict.Keys.Contains<string>(childNode.Attributes["key"].Value))
                {
                    key = childNode.Attributes["key"].Value;
                    xmlConfigModel = new XmlConfigModel() { Key = childNode.Attributes["key"].Value, Value = childNode.Attributes["value"].Value, Text = childNode.InnerText };
                    dict.Add(key, xmlConfigModel);
                }
               
            }

        }
        #region set/get Value
        public void SetValue(string keyName, string value)
        {
            if (dict.Keys.Contains<string>(keyName))
            {
                XmlNode singleNode = doc.SelectSingleNode(string.Format("//add[@key='{0}']", keyName));
               singleNode.Attributes["value"].Value = value;
               doc.Save(fileName);
            }
            else
            {
                throw new ArgumentException(string.Format("the input keyName:{0} was not been found", keyName));
            }
        }

        public string GetValue(string keyName)
        {
            if (dict.Keys.Contains<string>(keyName))
            {
                return dict[keyName].Value;
            }
            else
            {
                throw new ArgumentException(string.Format("the input keyName:{0} was not been found",keyName));
            }
        }
        #endregion

        #region set/get text
        public void SetText(string keyName, string text)
        {
            if (dict.Keys.Contains<string>(keyName))
            {
                XmlNode singleNode = doc.SelectSingleNode(string.Format("//add[@key='{0}']", keyName));
                singleNode.InnerText = text;
                doc.Save(fileName);
            }
            else
            {
                throw new ArgumentException(string.Format("the input keyName:{0} was not been found", keyName));
            }
        }

        public string GetText(string keyName)
        {
            if (dict.Keys.Contains<string>(keyName))
            {
                return dict[keyName].Text;
            }
            else
            {
                throw new ArgumentException(string.Format("the input keyName:{0} was not been found", keyName));
            }
        }
        #endregion

        public string ConfigXml { get; set; }
    }

    public sealed class XmlConfigModel
    {
        public string Key { get; set; }
        public string Value { get; set; }
        public string Text { get; set; }

    }
}
